package cn.mrcode.fd.blog.controller;

import cn.mrcode.fd.blog.common.BaseController;
import cn.mrcode.fd.blog.common.BusConstant;
import cn.mrcode.fd.blog.entity.Category;
import cn.mrcode.fd.blog.entityCu.ArticleCu;
import cn.mrcode.fd.blog.entityCu.UserCu;
import cn.mrcode.fd.blog.services.IArticleService;
import cn.mrcode.fd.blog.services.ICategoryService;
import cn.mrcode.fd.utils.mybatis.page.Page;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author : zhuqiang
 * @version : V1.0
 * @date : 2016/3/29 21:45
 */
@RequestMapping("/auth/manage")
@Controller
public class ManageController extends BaseController {
    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private ICategoryService iCategoryService;

    @RequestMapping("/postlist/{statu}")
    public String articleList(Model model,Integer pageNo,@PathVariable int statu) {
        UserCu user = getUser();
        pageNo = pageNo == null? 1:pageNo;

        Page<ArticleCu> page = new Page<ArticleCu>();
        page.setPageNo(pageNo);
        page.setPageSize(20);
        Page<ArticleCu> articlePage = iArticleService.queryAll(page,user.getAccount(), statu);

        model.addAttribute("user", user);
        model.addAttribute("articlePage",articlePage);
        model.addAttribute("statu", statu);
        if(BusConstant.ArticleStatus.NORMAL == statu){
            List<Category> categorys = iCategoryService.findAllByAccount(user.getAccount());
            model.addAttribute("categorys", JSON.toJSONString(categorys));
        }
        return "/manage/postlist";
    }

    @RequestMapping("/art/cats")
    public String artCats(Model model) {
        UserCu user = getUser();
        List<Category> cats = iCategoryService.findAllByAccount(user.getAccount());
        model.addAttribute("cats",cats);
        return "/manage/cats";
    }
}
